//
//  WMEMusicDownLoadManager.m
//  WMEMusic
//
//  Created by 恩叔 on 16/5/7.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEMusicDownLoadManager.h"

@implementation WMEMusicDownLoadManager
static WMEMusicDownLoadManager *sharedManager = nil;

+(WMEMusicDownLoadManager *)sharedManager{
    @synchronized(self) {
        if(sharedManager == nil) {
            sharedManager = [[self alloc]init];
        }
    }
    return sharedManager;
}

//- (void)downLoadWithUrlString:(NSString *)urlString
//                         name:(NSString *)name
//{
//    NSString * webPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Temp"];
//    NSString * cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Cache"];
//    NSLog(@"%@", webPath);
//    NSFileManager * fileManager = [NSFileManager defaultManager];
//    if(![fileManager fileExistsAtPath:webPath])
//    {
//        [fileManager createDirectoryAtPath:webPath withIntermediateDirectories:YES attributes:nil error:nil];
//    }
//    if (![fileManager fileExistsAtPath:cachePath]) {
//        [fileManager createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:nil];
//    }
//    if ([fileManager fileExistsAtPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",name]]]) {
//
//    }
//    else if([fileManager fileExistsAtPath:[webPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",name]]])
//    {
//
//    }
//    else
//    {
//        sharedManager.webPath = webPath;
//        sharedManager.url = urlString;
//        sharedManager.tempFile = [webPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",name]];
//        _request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
//        [_request setDownloadDestinationPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",name]]];
//        [_request setTemporaryFileDownloadPath:[webPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",name]]];
//        [_request setDelegate:self];
//        [_request setBytesReceivedBlock:^(unsigned long long size, unsigned long long total) {
//            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
//            [user setDouble:total forKey:@"file_length"];
//            sharedManager.size = size;
//            sharedManager.total = total;
//        }];
//        //_request.downloadProgressDelegate = sharedManager.myProgressView;
//        [_request setAllowResumeForFileDownloads:YES];
//        [_request startAsynchronous];
//    }
//
//}


- (void)downLoadWithBlock:(myBlock)block
                urlString:(NSString *)urlString
                     name:(NSString *)name
{
    NSString * webPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Temp"];
    NSString * cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Cache"];
    NSLog(@"%@", webPath);
    NSFileManager * fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:webPath])
    {
        [fileManager createDirectoryAtPath:webPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    if (![fileManager fileExistsAtPath:cachePath]) {
        [fileManager createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    if ([fileManager fileExistsAtPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",name]]]) {
        block(YES);
    }
    else if([fileManager fileExistsAtPath:[webPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",name]]])
    {
        block(YES);
    }
    else
    {
        sharedManager.webPath = webPath;
        sharedManager.url = urlString;
        sharedManager.tempFile = [webPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",name]];
        self.request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
        [_request setDownloadDestinationPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",name]]];
        [_request setTemporaryFileDownloadPath:[webPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",name]]];
        [_request setDelegate:self];
        [_request setBytesReceivedBlock:^(unsigned long long size, unsigned long long total) {
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            [user setDouble:total forKey:@"file_length"];
            sharedManager.size = size;
            sharedManager.total = total;
        }];
        //_request.downloadProgressDelegate = sharedManager.myProgressView;
        [_request setAllowResumeForFileDownloads:YES];
        [_request startAsynchronous];
        block(NO);
    }
}



- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    //NSString *responseString = [request responseString];
    
    // Use when fetching binary data
    // NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@",error);
}

@end
